package com.site.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.common.BaseController;
import com.entity.ProductCategory;
import com.service.ContactItemsService;
import com.service.ProductCategoryService;
import com.service.ProductionService;

@Controller
@RequestMapping("/")
public class ProductController extends BaseController {
	@Autowired
	private ProductCategoryService productCategoryService;
	@Autowired
	private ProductionService productionService;
	@Autowired
	private ContactItemsService contactItemsService;
	@RequestMapping("/product/p{pageNo}")
	public String index(HttpServletRequest request, @PathVariable Integer pageNo){
		this.initCommon(request, productCategoryService, contactItemsService, productionService);
		if(pageNo == null || pageNo < 1){
			pageNo = 1;
		}
		int total = this.productCategoryService.findAllCount();
		if(pageNo > total){
			pageNo = total;
		}
		List<ProductCategory> page = this.productCategoryService.findAllCategoryInPage(5*(pageNo-1), 5);
		for(ProductCategory category:page){
			category.setProducts(this.productionService.findByCategoryInPage(category.getId(), 0, 4));
			category.setSize(this.productionService.countByCategory(category.getId()));
		}
		request.setAttribute("page", page);
		
		request.setAttribute("total", total);
		request.setAttribute("pageNo", pageNo);
		request.setAttribute("pageNum", (total-1)/6+1);
		return "site.product";
	}
}
